package com.woniuxy.book.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.book.controller.form.useraddress.AddAddressForm;
import com.woniuxy.book.controller.form.useraddress.UpdateAddressForm;
import com.woniuxy.book.entity.PageInfo;
import com.woniuxy.book.entity.UserAddress;
import com.woniuxy.book.param.useraddress.AddAddressParam;
import com.woniuxy.book.param.useraddress.UpdateAddressParam;
import com.woniuxy.book.service.UserAddressService;
import com.woniuxy.book.util.JWTUtil;
import com.woniuxy.book.util.RedisUtil;
import com.woniuxy.book.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author li
 * @since 2024年2月09日
 */
@RestController
@RequestMapping("/userAddress")
public class UserAddressController {

    @Resource
    private RedisUtil redisUtil;
    @Resource
    private UserAddressService userAddressService;

    @GetMapping("/user/findDefaultAddress")
    public Result findDefaultAddress(@RequestHeader("token")String token){
        String refreshToken = redisUtil.get(token);
        Integer userId = (Integer) JWTUtil.getSubject(refreshToken);
        UserAddress userAddress = userAddressService.findDefaultAddress(userId);
        return Result.success("查找默认地址成功",userAddress);
    }

    @PostMapping("/user/addAddress")
    public Result addAddress(@RequestBody AddAddressForm form, @RequestHeader("token")String token){
        String refreshToken = redisUtil.get(token);
        Integer userId = (Integer) JWTUtil.getSubject(refreshToken);
        AddAddressParam param = BeanUtil.toBean(form, AddAddressParam.class);
        param.setUserId(userId);
        userAddressService.addAddress(param);
        return Result.success("新增地址成功",null);
    }

    @GetMapping("/user/selectAddress")
    public Result selectAddress(@RequestHeader("token")String token){
        String refreshToken = redisUtil.get(token);
        Integer userId = (Integer) JWTUtil.getSubject(refreshToken);
        List<UserAddress> userAddresses = userAddressService.selectAddress(userId);
        return Result.success("查询所有地址成功",userAddresses);
    }

    @GetMapping("/user/findAddress")
    public Result findAddress(Integer addressId,@RequestHeader("token")String token){
        String refreshToken = redisUtil.get(token);
        Integer userId = (Integer) JWTUtil.getSubject(refreshToken);
        UserAddress userAddress = userAddressService.findAddress(addressId,userId);
        return Result.success("改变收货地址成功",userAddress);
    }

    @GetMapping("/user/selectAddressPage")
    public Result selectAddressPage(Integer pageNum,Integer pageSize,@RequestHeader("token")String token){
        String refreshToken = redisUtil.get(token);
        Integer userId = (Integer) JWTUtil.getSubject(refreshToken);
        PageInfo pageInfo = userAddressService.selectAddressPage(pageNum,pageSize,userId);
        return Result.success("分页查询所有地址成功",pageInfo);
    }

    @GetMapping("/user/changeAddressState")
    public Result changeAddressState(Integer addressId,Integer addressState,@RequestHeader("token")String token){
        String refreshToken = redisUtil.get(token);
        Integer userId = (Integer) JWTUtil.getSubject(refreshToken);
        userAddressService.changeAddressState(addressId,addressState,userId);
        return Result.success("修改为默认地址成功",null);
    }

    @PostMapping("/user/updateAddress")
    public Result updateAddress(@RequestBody UpdateAddressForm form, @RequestHeader("token")String token){
        String refreshToken = redisUtil.get(token);
        Integer userId = (Integer) JWTUtil.getSubject(refreshToken);
        UpdateAddressParam param = BeanUtil.toBean(form, UpdateAddressParam.class);
        param.setUserId(userId);
        userAddressService.updateAddress(param);
        return Result.success("修改地址信息成功",null);
    }

    @GetMapping("/user/delAddress")
    public Result delAddress(Integer addressId,@RequestHeader("token")String token){
        String refreshToken = redisUtil.get(token);
        Integer userId = (Integer) JWTUtil.getSubject(refreshToken);
        userAddressService.delAddress(addressId,userId);
        return Result.success("删除地址成功",null);
    }

}

